﻿/*
=======================================================
    UsingWirerope【在用绳档案】
=======================================================
*/

using Entities;
using Microsoft.AspNetCore.Hosting;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace Services
{
    public interface IUsingWireropeLogic : ILogic<UsingWireropeModel, UsingWireropeFilterModel>
    {
        PageModel<UsingWireropeModel> GetStatusMonitorPage(UsingWireropeFilterModel filterModel, int pageIndex, int pageSize, string orderBy);
        UsingWireropeModel GetStatusMonitorByID(int id);
        UsingWireropeModel GetStatusMonitoryHistoryByID(int id);
        HomeSummaryModel GetHomeData(UsingWireropeFilterModel filterModel);
        string ExportLastCheckExcel(int id);
        void UpdateZuoFeiInfo(UsingWireropeModel model);
    }


    public class UsingWireropeLogic : BaseLogic<UsingWireropeModel, UsingWireropeFilterModel, UsingWirerope, IUsingWireropeRepository, IUsingWireropeMapping, IUsingWireropeService>, IUsingWireropeLogic
    {
        #region Ioc
        private readonly ICheckWireropeStatusMonitorRepository _ICheckWireropeStatusMonitorRepository;
        private readonly ICheckWireropeStatusMonitorMapping _ICheckWireropeStatusMonitorMapping;

        private readonly IHoisterRepository _IHoisterRepository;
        private readonly IHoisterMapping _IHoisterMapping;

        private readonly ICheckWireropeRepository _ICheckWireropeRepository;
        private readonly ICheckWireropeMapping _ICheckWireropeMapping;

        private readonly ICheckWireropeDetailRepository _ICheckWireropeDetailRepository;
        //private readonly ICheckWireropeDetailMapping _ICheckWireropeDetailMapping;

        private readonly ICheckWireropeDetailDataRepository _ICheckWireropeDetailDataRepository;
        //private readonly ICheckWireropeDetailMapping _CheckWireropeDetailMapping;

        private readonly ICheckWireropeDetailPositionRepository _ICheckWireropeDetailPositionRepository;
        //private readonly ICheckWireropeDetailPositionMapping _ICheckWireropeDetailPositionMapping;

        private readonly IUsingWireropeKeepRepository _IUsingWireropeKeepRepository;
        private readonly IUsingWireropeKeepMapping _IUsingWireropeKeepMapping;

        private readonly IStockRepository _IStockRepository;

        private readonly IBillMapping _IBillMapping;
        private readonly IBillRepository _IBillRepository;

        private readonly IStockWireropeKeepRepository _IStockWireropeKeepRepository;
        private readonly IStockWireropeKeepMapping _IStockWireropeKeepMapping;

        private readonly IBillDetailRepository _IBillDetailRepository;
        private readonly IChangeWireropeRepository _IChangeWireropeRepository;
        private readonly ICreateCode _ICreateCode;
        private readonly IOperTypeRepository _IOperTypeRepository;
        private readonly IJudgeWireropeStatusMonitor _IJudgeWireropeStatusMonitor;

        private readonly IWebHostEnvironment _webHostEnvironment;

        private readonly IStandStatusDetailRepository _IStandStatusDetailRepository;
        private readonly IStandStatusRepository _IStandStatusRepository;

        public UsingWireropeLogic(ICurrentUser currentUser, IUsingWireropeRepository repository, IUsingWireropeMapping mapping, IUsingWireropeService service,

            ICheckWireropeStatusMonitorRepository iCheckWireropeStatusMonitorRepository, ICheckWireropeStatusMonitorMapping iCheckWireropeStatusMonitorMapping,

            IHoisterRepository iHoisterRepository, IHoisterMapping iHoisterMapping,

            ICheckWireropeRepository iCheckWireropeRepository, ICheckWireropeMapping iCheckWireropeMapping,

            ICheckWireropeDetailRepository iCheckWireropeDetailRepository,

            ICheckWireropeDetailDataRepository iCheckWireropeDetailDataRepository,

            ICheckWireropeDetailPositionRepository iCheckWireropeDetailPositionRepository,

            IUsingWireropeKeepRepository iUsingWireropeKeepRepository, IUsingWireropeKeepMapping iUsingWireropeKeepMapping,
            IStockRepository __IStockRepository,
            IBillMapping __IBillMapping,
            IBillRepository __IBillRepository,
            IStockWireropeKeepRepository __IStockWireropeKeepRepository,
            IStockWireropeKeepMapping __IStockWireropeKeepMapping,
            IChangeWireropeRepository __IChangeWireropeRepository,
            IBillDetailRepository __IBillDetailRepository, ICreateCode iCreateCode, IOperTypeRepository iOperTypeRepository, IJudgeWireropeStatusMonitor iJudgeWireropeStatusMonitor, IWebHostEnvironment webHostEnvironment, IStandStatusDetailRepository iStandStatusDetailRepository, IStandStatusRepository iStandStatusRepository)
            : base(currentUser, repository, mapping, service)
        {
            _ICheckWireropeStatusMonitorRepository = iCheckWireropeStatusMonitorRepository;
            _ICheckWireropeStatusMonitorMapping = iCheckWireropeStatusMonitorMapping;

            _IHoisterRepository = iHoisterRepository;
            _IHoisterMapping = iHoisterMapping;

            _ICheckWireropeRepository = iCheckWireropeRepository;
            _ICheckWireropeMapping = iCheckWireropeMapping;

            _ICheckWireropeDetailRepository = iCheckWireropeDetailRepository;

            _ICheckWireropeDetailDataRepository = iCheckWireropeDetailDataRepository;

            _ICheckWireropeDetailPositionRepository = iCheckWireropeDetailPositionRepository;

            _IUsingWireropeKeepRepository = iUsingWireropeKeepRepository;
            _IUsingWireropeKeepMapping = iUsingWireropeKeepMapping;

            _IStockRepository = __IStockRepository;

            _IBillRepository = __IBillRepository;
            _IBillMapping = __IBillMapping;

            _IBillDetailRepository = __IBillDetailRepository;
            _IChangeWireropeRepository = __IChangeWireropeRepository;

            _IStockWireropeKeepRepository = __IStockWireropeKeepRepository;
            _IStockWireropeKeepMapping = __IStockWireropeKeepMapping;
            _ICreateCode = iCreateCode;
            _IOperTypeRepository = iOperTypeRepository;
            _IJudgeWireropeStatusMonitor = iJudgeWireropeStatusMonitor;
            _webHostEnvironment = webHostEnvironment;
            _IStandStatusDetailRepository = iStandStatusDetailRepository;
            _IStandStatusRepository = iStandStatusRepository;
        }
        #endregion

        #region 添加在用绳
        public override UsingWireropeModel Insert(UsingWireropeModel model)
        {
            //新增
            model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
            model.Status = (int)UsingWirerope_Status.在用;
            model.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.CreateDate = DateTime.Now;
            model.UpdateDate = DateTime.Now;
            model.UpdateUserID = _currentUser.CurrentUserInfo.UserID;
            model.UpdateUserName = _currentUser.CurrentUserInfo.UserName;

            var stockInfo = _IStockRepository.GetByID(model.StockID.Value);

            if (stockInfo == null)
            {
                throw new CustomException("该绳子不存在！");
            }


            //建立出库单
            //建立BillDetail和Bill数据
            Bill bill = new Bill();
            bill.GroupInfoID = model.GroupInfoID;
            bill.Code = _ICreateCode.GetBillCode(Bill_InOutType.出库);
            bill.BillDate = model.OutStockDate;
            bill.InOutType = (int)Bill_InOutType.出库;

            var where3 = Expressionable.Create<OperType>();
            where3 = where3.And(x => x.GroupInfoID == model.GroupInfoID);
            where3 = where3.And(x => x.IsInit == true);
            where3 = where3.And(x => x.InOutType == (int)Bill_InOutType.出库);
            var temp = _IOperTypeRepository.GetModel(where3);
            if (temp == null)
            {
                throw new CustomException("出入库类型不存在，联系管理员！");
            }

            bill.OperTypeID = temp.OperTypeID;
            bill.WarehouseID = stockInfo.WarehouseID;
            bill.ApplyUserID = _currentUser.CurrentUserInfo.UserID;
            bill.ApplyUserName = _currentUser.CurrentUserInfo.UserName;
            bill.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
            bill.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
            bill.SubmitDate = model.OutStockDate;
            bill.ConfirmUserID = _currentUser.CurrentUserInfo.UserID;
            bill.ConfirmUserName = _currentUser.CurrentUserInfo.UserName;
            bill.ConfirmDate = model.OutStockDate;
            bill.Status = (int)Bill_Status.已确认;
            bill.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            bill.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            bill.CreateDate = model.OutStockDate;


            BillDetail billDetail = new BillDetail();
            billDetail.GroupInfoID = model.GroupInfoID;
            billDetail.Code = stockInfo.Code;
            billDetail.WarehouseID = stockInfo.WarehouseID;
            billDetail.WarehouseName = stockInfo.WarehouseName;
            billDetail.WarehousePositionID = stockInfo.WarehousePositionID;
            billDetail.WarehousePositionName = stockInfo.WarehousePositionName;
            billDetail.GuiGe = stockInfo.Code;
            billDetail.Diameter = stockInfo.Diameter;
            billDetail.Cnt = stockInfo.Cnt;
            billDetail.Name = stockInfo.Name;
            billDetail.NianJu = stockInfo.NianJu;
            billDetail.MpaID = stockInfo.MpaID;
            billDetail.MpaName = stockInfo.MpaName;
            billDetail.SurfaceStatusID = stockInfo.SurfaceStatusID;
            billDetail.SurfaceStatusName = stockInfo.SurfaceStatusName;
            billDetail.TwistingTypeID = stockInfo.TwistingTypeID;
            billDetail.TwistingTypeName = stockInfo.TwistingTypeName;
            billDetail.LengthNum = stockInfo.LengthNum;
            billDetail.KeepDay = stockInfo.KeepDay;
            billDetail.SupplierID = stockInfo.SupplierID;
            billDetail.SupplierName = stockInfo.SupplierName;
            billDetail.ProducerID = stockInfo.ProducerID;
            billDetail.ProducerName = stockInfo.ProducerName;
            billDetail.ChuChang_Files = stockInfo.ChuChang_Files;
            billDetail.YanShou_Files = stockInfo.YanShou_Files;
            billDetail.JianCe_Files = stockInfo.JianCe_Files;
            billDetail.Attr_Files = stockInfo.Attr_Files;
            billDetail.DepartmentID = stockInfo.DepartmentID;
            billDetail.HoisterID = stockInfo.HoisterID;
            billDetail.HoisterName = stockInfo.HoisterName;
            billDetail.HoisterUsageID = stockInfo.HoisterUsageID;
            billDetail.HoisterUsageName = stockInfo.HoisterUsageName;
            billDetail.WireropeTypeID = stockInfo.WireropeTypeID;
            billDetail.WireropeTypeName = stockInfo.WireropeTypeName;
            billDetail.Remark = stockInfo.Remark;


            stockInfo.IsOut = true;

            _repository.InsertForAdmin(_mapping.Model2Entity(model), bill, billDetail, stockInfo);

            return model;
        }
        #endregion

        #region 修改
        public override int Update(UsingWireropeModel model)
        {
            var oldInfo = GetModel(new UsingWireropeFilterModel { _UsingWireropeID = model.UsingWireropeID });

            model.UpdateDate = DateTime.Now;
            model.UpdateUserID = _currentUser.CurrentUserInfo.UserID;
            model.UpdateUserName = _currentUser.CurrentUserInfo.UserName;

            var temp = _repository.Update(_mapping.Model2Entity(model));

            //判断这次更改是否更改提升机用途、提升机类型、钢丝绳用途,如果有，并且有检验数据，需要更新灯的状态
            if (oldInfo.HoisterID != model.HoisterID || oldInfo.HoisterUsageID != model.HoisterUsageID || oldInfo.WireropeTypeID != model.WireropeTypeID)
            {
                var tempInfo = _IJudgeWireropeStatusMonitor.JudgeWireropeMonitor(model.UsingWireropeID);

                _IJudgeWireropeStatusMonitor.InsertOrUpdateWireropeMonitor(tempInfo);
            }

            return temp;
        }
        #endregion

        #region GetByID
        public override UsingWireropeModel GetByID(int id)
        {
            var model = GetModel(new UsingWireropeFilterModel { _UsingWireropeID = id });

            //单据
            var where2 = Expressionable.Create<BillDetail>();
            where2 = where2.And(x => x.Code == model.Code);
            var detailInfo = _IBillDetailRepository.GetModel(where2);

            if (detailInfo != null)
            {
                var bill_where = Expressionable.Create<Bill>();
                bill_where = bill_where.And(x => x.BillID == detailInfo.BillID);
                var billInfo = _IBillMapping.Entity2Model(_IBillRepository.GetModel(bill_where));
                model.BillModel = billInfo;
            }

            //备用绳维保信息
            var where3 = Expressionable.Create<StockWireropeKeep>();
            where3 = where3.And(x => x.StockID == model.StockID);
            model.StockWireropeKeepModelList = _IStockWireropeKeepMapping.Entity2Model(_IStockWireropeKeepRepository.GetSearch(where3).ToList());


            //安装信息
            var where4 = Expressionable.Create<ChangeWirerope>();
            where4 = where4.And(x => x.NewUsingWireropeID == model.UsingWireropeID);
            var changeInfo = _IChangeWireropeRepository.GetModel(where4);
            InstallModel installModel = new InstallModel();

            installModel.InstallTime = model.InstallDate;
            installModel.Remark = model.Remark;

            if (changeInfo != null)
            {
                installModel.Type = (int)InstallType_Status.换绳;
                installModel.CreateUserName = changeInfo.CreateUserName;
                installModel.Reason = changeInfo.Reason;
            }
            else
            {
                installModel.Type = (int)InstallType_Status.新建;
                installModel.CreateUserName = model.CreateUserName;
            }
            model.InstallModel = installModel;


            //在用绳检测
            model.CheckWireropeModelList = _ICheckWireropeMapping.Entity2Model(_ICheckWireropeRepository.GetAllCheckWirerope(id).OrderBy(x => x.CheckDate).ToList());


            //在用绳维保
            var where5 = Expressionable.Create<UsingWireropeKeep>();
            where5 = where5.And(x => x.UsingWireropeID == model.UsingWireropeID);
            model.UsingWireropeKeepModelList = _IUsingWireropeKeepMapping.Entity2Model(_IUsingWireropeKeepRepository.GetSearch(where5).ToList());


            //作废信息
            if (model.Status == (int)UsingWirerope_Status.作废)
            {
                ZuoFeiModel zuoFeiModel = new ZuoFeiModel();
                zuoFeiModel.Remark = model.Remark;

                var where6 = Expressionable.Create<ChangeWirerope>();
                where6 = where6.And(x => x.OldUsingWireropeID == model.UsingWireropeID);
                var changeInfo2 = _IChangeWireropeRepository.GetModel(where6);
                zuoFeiModel.AutoID = changeInfo2.ChangeWireropeID;
                zuoFeiModel.CreateUserName = changeInfo2.CreateUserName;
                zuoFeiModel.ZuoFeiTime = changeInfo2.InstallDate;
                zuoFeiModel.Reason = changeInfo2.Reason;

                model.ZuoFeiModel = zuoFeiModel;
            }

            return model;
        }
        #endregion

        #region 状态监测列表
        public PageModel<UsingWireropeModel> GetStatusMonitorPage(UsingWireropeFilterModel filterModel, int pageIndex, int pageSize, string orderBy)
        {
            var pageData = GetPage(filterModel, pageIndex, pageSize, orderBy);
            var items = pageData.Items;

            var ids = items.Select(x => x.UsingWireropeID).ToList();

            var where = _ICheckWireropeStatusMonitorMapping.FilterModel2Where(new CheckWireropeStatusMonitorFilterModel());
            where = where.And(x => ids.Contains(x.UsingWireropeID));
            var statusMonitors = _ICheckWireropeStatusMonitorRepository.GetSearch(where);

            var where2 = _ICheckWireropeMapping.FilterModel2Where(new CheckWireropeFilterModel());
            where2 = where2.And(x => ids.Contains(x.UsingWireropeID) && x.Status == (int)CheckWirerope_Status.已检测);
            var checkWireropes = _ICheckWireropeRepository.GetSearch(where2);

            foreach (var item in items)
            {
                item.CheckWireropeStatusMonitorModel = _ICheckWireropeStatusMonitorMapping.Entity2Model(statusMonitors.FirstOrDefault(x => x.UsingWireropeID == item.UsingWireropeID));
                item.HasCheck = checkWireropes.Any(x => x.UsingWireropeID == item.UsingWireropeID);
            }

            return pageData;
        }
        #endregion

        #region 状态监测详情
        public UsingWireropeModel GetStatusMonitorByID(int id)
        {
            var model = GetModel(new UsingWireropeFilterModel { _UsingWireropeID = id });

            var standStatus = _IStandStatusRepository.GetAll();
            //几种状态的备注
            model.LaRemark = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.LA)?.Remark;
            model.DiaRemark = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.DIA)?.Remark;
            model.LmaRemark = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.LMA)?.Remark;
            model.NianJuRemark = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.捻距变化)?.Remark;

            //当前最后一次检测数据
            var lastCheck = _ICheckWireropeRepository.GetLastCheckWirerope(id);

            if (lastCheck.IsNotEmpty())
            {
                //得到所有检测绳子信息
                lastCheck.CheckWireropeDetails = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(lastCheck.CheckWireropeID);

                var detailIDs = lastCheck.CheckWireropeDetails.Select(x => x.CheckWireropeDetailID).ToList();

                //得到数据录入和位置这2个集合
                var cwdds = _ICheckWireropeDetailDataRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());
                var cwdps = _ICheckWireropeDetailPositionRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());

                foreach (var item in lastCheck.CheckWireropeDetails)
                {
                    item.CheckWireropeDetailDatas = cwdds.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
                    item.CheckWireropeDetailPositions = cwdps.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
                }

                model.LastCheckWireropeModel = _ICheckWireropeMapping.Entity2Model(lastCheck);
            }

            //得到最后一次维保
            model.LastUsingWireropeKeepModel = _IUsingWireropeKeepMapping.Entity2Model(_IUsingWireropeKeepRepository.GetLastKeepWirerope(id));

            //得到提升机数据
            model.HoisterModel = _IHoisterMapping.Entity2Model(_IHoisterRepository.GetByID(model.HoisterID));

            return model;
        }
        #endregion

        #region 状态监测历史记录
        public UsingWireropeModel GetStatusMonitoryHistoryByID(int id)
        {
            var model = GetModel(new UsingWireropeFilterModel { _UsingWireropeID = id });
            model.CheckWireropeHistoryList = new List<CheckWireropeHistoryModel>();

            //得到当前绳子的所有检测记录
            var allChecks = _ICheckWireropeRepository.GetAllCheckWirerope(id).OrderBy(x => x.CheckDate).ToList();
            //得到所有检测记录的详情
            var allDetails = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(allChecks.Select(x => x.CheckWireropeID).ToArray());

            //要怕检测记录构建出3处tab页的信息
            var x = allChecks.Select(x => x.CheckDate.Value).ToList();
            var la = new CheckWireropeHistoryModel { TabName = "LA", X_Date = x };
            var dia = new CheckWireropeHistoryModel { TabName = "DIA", X_Date = x };
            var lma = new CheckWireropeHistoryModel { TabName = "LMA", X_Date = x };
            var nianJu = new CheckWireropeHistoryModel { TabName = "捻距变化", X_Date = x };

            //循环检测记录
            foreach (var check in allChecks)
            {
                //根据每次的检测记录获取检测详情，即有几根绳
                var _details = allDetails.Where(x => x.CheckWireropeID == check.CheckWireropeID).ToList();

                //循环检测详情
                foreach (var detail in _details)
                {
                    //构建la
                    var laItem = la.Items.FirstOrDefault(x => x.Name == detail.Name);
                    if (laItem.IsEmpty())
                    {
                        la.Items.Add(new CheckWireropeHistoryDataModel { Name = detail.Name, Num = new List<decimal> { detail.DsLa.Value } });
                    }
                    else
                    {
                        la.Items.First(x => x.Name == detail.Name).Num.Add(detail.DsLa.Value);
                    }

                    //构建dia
                    var diaItem = dia.Items.FirstOrDefault(x => x.Name == detail.Name);
                    if (diaItem.IsEmpty())
                    {
                        dia.Items.Add(new CheckWireropeHistoryDataModel { Name = detail.Name, Num = new List<decimal> { detail.ZjDia.Value } });
                    }
                    else
                    {
                        dia.Items.First(x => x.Name == detail.Name).Num.Add(detail.ZjDia.Value);
                    }

                    //构建lma
                    var lmaItem = lma.Items.FirstOrDefault(x => x.Name == detail.Name);
                    if (lmaItem.IsEmpty())
                    {
                        lma.Items.Add(new CheckWireropeHistoryDataModel { Name = detail.Name, Num = new List<decimal> { detail.JsjmLma.Value } });
                    }
                    else
                    {
                        lma.Items.First(x => x.Name == detail.Name).Num.Add(detail.JsjmLma.Value);
                    }


                    //构建捻距变化
                    var nianJuItem = nianJu.Items.FirstOrDefault(x => x.Name == detail.Name);
                    if (nianJuItem.IsEmpty())
                    {
                        nianJu.Items.Add(new CheckWireropeHistoryDataModel { Name = detail.Name, Num = new List<decimal> { detail.NianJuChange.Value } });
                    }
                    else
                    {
                        nianJu.Items.First(x => x.Name == detail.Name).Num.Add(detail.NianJuChange.Value);
                    }
                }
            }

            model.CheckWireropeHistoryList.Add(la);
            model.CheckWireropeHistoryList.Add(dia);
            model.CheckWireropeHistoryList.Add(lma);
            model.CheckWireropeHistoryList.Add(nianJu);
            return model;
        }
        #endregion

        #region 首页
        public HomeSummaryModel GetHomeData(UsingWireropeFilterModel filterModel)
        {
            var allData = _ICheckWireropeStatusMonitorRepository.GetInUsedAll(filterModel._DepartmentID);

            var zhongHe = new HomeSummaryItemModel
            {
                Green = allData.Count(x => x.Summary.HasValue && x.Summary.Value == (int)StatusMonitor_LightType.绿),
                Blue = allData.Count(x => x.Summary.HasValue && x.Summary.Value == (int)StatusMonitor_LightType.蓝),
                Yellow = allData.Count(x => x.Summary.HasValue && x.Summary.Value == (int)StatusMonitor_LightType.黄),
                Orange = allData.Count(x => x.Summary.HasValue && x.Summary.Value == (int)StatusMonitor_LightType.橙),
                Grey = allData.Count(x => x.Summary.HasValue && x.Summary.Value == (int)StatusMonitor_LightType.灰),
            };

            var la = new HomeSummaryItemModel
            {
                Green = allData.Count(x => x.La.HasValue && x.La.Value == (int)StatusMonitor_LightType.绿),
                Blue = allData.Count(x => x.La.HasValue && x.La.Value == (int)StatusMonitor_LightType.蓝),
                Yellow = allData.Count(x => x.La.HasValue && x.La.Value == (int)StatusMonitor_LightType.黄),
                Orange = allData.Count(x => x.La.HasValue && x.La.Value == (int)StatusMonitor_LightType.橙),
                Grey = allData.Count(x => x.La.HasValue && x.La.Value == (int)StatusMonitor_LightType.灰),
            };

            var dia = new HomeSummaryItemModel
            {
                Green = allData.Count(x => x.Dia.HasValue && x.Dia.Value == (int)StatusMonitor_LightType.绿),
                Blue = allData.Count(x => x.Dia.HasValue && x.Dia.Value == (int)StatusMonitor_LightType.蓝),
                Yellow = allData.Count(x => x.Dia.HasValue && x.Dia.Value == (int)StatusMonitor_LightType.黄),
                Orange = allData.Count(x => x.Dia.HasValue && x.Dia.Value == (int)StatusMonitor_LightType.橙),
                Grey = allData.Count(x => x.Dia.HasValue && x.Dia.Value == (int)StatusMonitor_LightType.灰),
            };

            var lma = new HomeSummaryItemModel
            {
                Green = allData.Count(x => x.Lma.HasValue && x.Lma.Value == (int)StatusMonitor_LightType.绿),
                Blue = allData.Count(x => x.Lma.HasValue && x.Lma.Value == (int)StatusMonitor_LightType.蓝),
                Yellow = allData.Count(x => x.Lma.HasValue && x.Lma.Value == (int)StatusMonitor_LightType.黄),
                Orange = allData.Count(x => x.Lma.HasValue && x.Lma.Value == (int)StatusMonitor_LightType.橙),
                Grey = allData.Count(x => x.Lma.HasValue && x.Lma.Value == (int)StatusMonitor_LightType.灰),
            };

            var nianJu = new HomeSummaryItemModel
            {
                Green = allData.Count(x => x.NianJu.HasValue && x.NianJu.Value == (int)StatusMonitor_LightType.绿),
                Blue = allData.Count(x => x.NianJu.HasValue && x.NianJu.Value == (int)StatusMonitor_LightType.蓝),
                Yellow = allData.Count(x => x.NianJu.HasValue && x.NianJu.Value == (int)StatusMonitor_LightType.黄),
                Orange = allData.Count(x => x.NianJu.HasValue && x.NianJu.Value == (int)StatusMonitor_LightType.橙),
                Grey = allData.Count(x => x.NianJu.HasValue && x.NianJu.Value == (int)StatusMonitor_LightType.灰),
            };

            return new HomeSummaryModel { ZhongHe = zhongHe, La = la, Dia = dia, Lma = lma, NianJu = nianJu };
        }
        #endregion

        #region 导出
        public string ExportLastCheckExcel(int id)
        {
            //获取最一次检测记录
            var lastCheck = _ICheckWireropeRepository.GetLastCheckWirerope(id);
            if (lastCheck.IsEmpty())
            {
                throw new CustomException("无检测数据，导出失败！");
            }

            //文件夹
            string floder = "Upload/Export/";
            string uploadFloder = Path.Combine(_webHostEnvironment.ContentRootPath, floder);

            //文件
            string fileName = string.Format("{0}{1}", floder, Gx.GetCode("检测分析报告") + ".xlsx");
            string uploadFile = Path.Combine(_webHostEnvironment.ContentRootPath, fileName);


            //判断文件夹路径是否存在，不存在则创建
            if (!Directory.Exists(uploadFloder))
            {
                //创建文件夹路径
                Directory.CreateDirectory(uploadFloder);
            }

            //获取在用绳
            var where = Expressionable.Create<UsingWirerope>();
            where = where.And(x => x.UsingWireropeID == id);
            var usingWirerope = _repository.GetModel(where);


            var cwd_where = Expressionable.Create<CheckWireropeDetail>();
            lastCheck.CheckWireropeDetails = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(lastCheck.CheckWireropeID);

            var detailIDs = lastCheck.CheckWireropeDetails.Select(x => x.CheckWireropeDetailID).ToList();

            var cwdps = _ICheckWireropeDetailPositionRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());

            foreach (var item in lastCheck.CheckWireropeDetails)
            {
                item.CheckWireropeDetailPositions = cwdps.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
            }

            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");

            #region 设置列宽
            sheet.SetColumnWidth(0, Gx.GetExcelColWeight(62));
            sheet.SetColumnWidth(1, Gx.GetExcelColWeight(98));
            sheet.SetColumnWidth(2, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(3, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(4, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(5, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(6, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(7, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(8, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(9, Gx.GetExcelColWeight(58));
            sheet.SetColumnWidth(10, Gx.GetExcelColWeight(66));
            sheet.SetColumnWidth(11, Gx.GetExcelColWeight(100));
            sheet.SetColumnWidth(12, Gx.GetExcelColWeight(100));
            sheet.SetColumnWidth(13, Gx.GetExcelColWeight(100));
            sheet.SetColumnWidth(14, Gx.GetExcelColWeight(100));
            #endregion

            int rowIndex = 0;

            #region row1
            IRow row1 = sheet.CreateRow(rowIndex++);
            row1.Height = Gx.GetExcelRowHeight(47);

            ICell A1 = row1.CreateCell(0);
            A1.SetCellValue($"{usingWirerope.Name}检测分析报表");
            A1.CellStyle = Gx.GetStyle(workbook, fontSize: 14, isBold: true);
            Gx.MergeCell(0, 0, 0, 14, sheet);
            #endregion

            #region row2
            IRow row2 = sheet.CreateRow(rowIndex++);
            row2.Height = Gx.GetExcelRowHeight(32);

            ICell A2 = row2.CreateCell(0);
            A2.SetCellValue("使用单位");
            A2.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(1, 1, 0, 4, sheet);

            ICell F2 = row2.CreateCell(5);
            F2.SetCellValue(usingWirerope.Department_Name);
            F2.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(1, 1, 5, 10, sheet);

            ICell J2 = row2.CreateCell(11);
            J2.SetCellValue("提升用途");
            J2.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(1, 1, 11, 12, sheet);

            ICell L2 = row2.CreateCell(13);
            L2.SetCellValue(usingWirerope.HoisterUsageName);
            L2.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(1, 1, 13, 14, sheet);
            #endregion

            #region row3
            IRow row3 = sheet.CreateRow(rowIndex++);
            row3.Height = Gx.GetExcelRowHeight(32);

            ICell A3 = row3.CreateCell(0);
            A3.SetCellValue("制造单位");
            A3.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(2, 2, 0, 4, sheet);

            ICell F3 = row3.CreateCell(5);
            F3.SetCellValue(usingWirerope.ProducerName);
            F3.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(2, 2, 5, 10, sheet);

            ICell J3 = row3.CreateCell(11);
            J3.SetCellValue("钢丝绳类型");
            J3.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(2, 2, 11, 12, sheet);

            ICell L3 = row3.CreateCell(13);
            L3.SetCellValue(usingWirerope.WireropeTypeName);
            L3.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(2, 2, 13, 14, sheet);
            #endregion

            #region row4
            IRow row4 = sheet.CreateRow(rowIndex++);
            row4.Height = Gx.GetExcelRowHeight(32);

            ICell A4 = row4.CreateCell(0);
            A4.SetCellValue("公称直径");
            A4.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(3, 3, 0, 4, sheet);

            ICell F4 = row4.CreateCell(5);
            F4.SetCellValue(usingWirerope.Diameter?.ToString("F2") + " mm");
            F4.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(3, 3, 5, 10, sheet);

            ICell J4 = row4.CreateCell(11);
            J4.SetCellValue("使用长度");
            J4.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(3, 3, 11, 12, sheet);

            ICell L4 = row4.CreateCell(13);
            L4.SetCellValue(usingWirerope.LengthNum?.ToString("F2") + " m");
            L4.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(3, 3, 13, 14, sheet);
            #endregion

            #region row5
            IRow row5 = sheet.CreateRow(rowIndex++);
            row5.Height = Gx.GetExcelRowHeight(32);

            ICell A5 = row5.CreateCell(0);
            A5.SetCellValue("规格型号");
            A5.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(4, 4, 0, 4, sheet);

            ICell F5 = row5.CreateCell(5);
            F5.SetCellValue(usingWirerope.GuiGe);
            F5.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(4, 4, 5, 10, sheet);

            ICell J5 = row5.CreateCell(11);
            J5.SetCellValue("安装日期");
            J5.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(4, 4, 11, 12, sheet);

            ICell L5 = row5.CreateCell(13);
            L5.SetCellValue(usingWirerope.InstallDate.ToString("yyyy-MM-dd"));
            L5.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(4, 4, 13, 14, sheet);
            #endregion

            #region row6
            IRow row6 = sheet.CreateRow(rowIndex++);
            row6.Height = Gx.GetExcelRowHeight(32);

            ICell A6 = row6.CreateCell(0);
            A6.SetCellValue("上次检测时间");
            A6.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(5, 5, 0, 4, sheet);

            ICell F6 = row6.CreateCell(5);
            F6.SetCellValue(lastCheck.CheckDate?.ToString("yyyy-MM-dd"));
            F6.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(5, 5, 5, 10, sheet);

            ICell J6 = row6.CreateCell(11);
            J6.SetCellValue("已投入使用");
            J6.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(5, 5, 11, 12, sheet);

            ICell L6 = row6.CreateCell(13);
            L6.SetCellValue($"{(DateTime.Now.ToString("yyyy-MM-dd").ToDateTime() - usingWirerope.InstallDate.ToString("yyyy-MM-dd").ToDateTime()).TotalDays} 天");
            L6.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(5, 5, 13, 14, sheet);
            #endregion

            #region row7
            IRow row7 = sheet.CreateRow(rowIndex++);
            row7.Height = Gx.GetExcelRowHeight(32);

            ICell A7 = row7.CreateCell(0);
            A7.SetCellValue("检查结果");
            A7.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(6, 6, 0, 14, sheet);
            #endregion

            #region row8
            IRow row8 = sheet.CreateRow(rowIndex++);
            row8.Height = Gx.GetExcelRowHeight(32);

            ICell A8 = row8.CreateCell(0);
            A8.SetCellValue("绳名称");
            A8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 8, 0, 0, sheet);

            ICell B8 = row8.CreateCell(1);
            B8.SetCellValue("断丝");
            B8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 7, 1, 4, sheet);

            ICell F8 = row8.CreateCell(5);
            F8.SetCellValue("直径");
            F8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 7, 5, 6, sheet);

            ICell H8 = row8.CreateCell(7);
            H8.SetCellValue("金属截面积损失");
            H8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 7, 7, 8, sheet);

            ICell J8 = row8.CreateCell(9);
            J8.SetCellValue("捻距变化");
            J8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 7, 9, 10, sheet);


            ICell L8 = row8.CreateCell(11);
            L8.SetCellValue("锈蚀情况");
            L8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 8, 11, 11, sheet);

            ICell M8 = row8.CreateCell(12);
            M8.SetCellValue("磨损情况");
            M8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 8, 12, 12, sheet);

            ICell N8 = row8.CreateCell(13);
            N8.SetCellValue("其他损伤");
            N8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 8, 13, 13, sheet);

            ICell O8 = row8.CreateCell(14);
            O8.SetCellValue("综合判定");
            O8.CellStyle = Gx.GetStyle(workbook);
            Gx.MergeCell(7, 8, 14, 14, sheet);
            #endregion

            #region row9
            IRow row9 = sheet.CreateRow(rowIndex++);
            row9.Height = Gx.GetExcelRowHeight(32);

            ICell A9 = row9.CreateCell(0);
            A9.CellStyle = Gx.GetStyle(workbook);

            ICell B9 = row9.CreateCell(1);
            B9.SetCellValue("捻距内断丝数");
            B9.CellStyle = Gx.GetStyle(workbook);

            ICell C9 = row9.CreateCell(2);
            C9.SetCellValue("位置起");
            C9.CellStyle = Gx.GetStyle(workbook);

            ICell D9 = row9.CreateCell(3);
            D9.SetCellValue("位置止");
            D9.CellStyle = Gx.GetStyle(workbook);

            ICell E9 = row9.CreateCell(4);
            E9.SetCellValue("LA");
            E9.CellStyle = Gx.GetStyle(workbook);

            ICell F9 = row9.CreateCell(5);
            F9.SetCellValue("位置");
            F9.CellStyle = Gx.GetStyle(workbook);

            ICell G9 = row9.CreateCell(6);
            G9.SetCellValue("DIA");
            G9.CellStyle = Gx.GetStyle(workbook);

            ICell H9 = row9.CreateCell(7);
            H9.SetCellValue("位置");
            H9.CellStyle = Gx.GetStyle(workbook);

            ICell I9 = row9.CreateCell(8);
            I9.SetCellValue("LMA");
            I9.CellStyle = Gx.GetStyle(workbook);

            ICell J9 = row9.CreateCell(9);
            J9.SetCellValue("捻距");
            J9.CellStyle = Gx.GetStyle(workbook);

            ICell K9 = row9.CreateCell(10);
            K9.SetCellValue("捻距变化");
            K9.CellStyle = Gx.GetStyle(workbook);

            ICell L9 = row9.CreateCell(11);
            L9.CellStyle = Gx.GetStyle(workbook);

            ICell M9 = row9.CreateCell(12);
            M9.CellStyle = Gx.GetStyle(workbook);

            ICell N9 = row9.CreateCell(13);
            N9.CellStyle = Gx.GetStyle(workbook);

            ICell O9 = row9.CreateCell(14);
            O9.CellStyle = Gx.GetStyle(workbook);
            #endregion

            #region row_data  循环
            foreach (var detail in lastCheck.CheckWireropeDetails)
            {
                //处理没有位置信息
                if (detail.CheckWireropeDetailPositions.IsEmpty() || detail.CheckWireropeDetailPositions.Count == 0)
                {
                    IRow row_data = sheet.CreateRow(rowIndex++);
                    row_data.Height = Gx.GetExcelRowHeight(32);

                    ICell A10 = row_data.CreateCell(0);
                    A10.SetCellValue(detail.Name);
                    A10.CellStyle = Gx.GetStyle(workbook);

                    ICell B10 = row_data.CreateCell(1);
                    B10.SetCellValue(detail.DsCnt?.ToString());
                    B10.CellStyle = Gx.GetStyle(workbook);

                    ICell C10 = row_data.CreateCell(2);
                    C10.SetCellValue("-");
                    C10.CellStyle = Gx.GetStyle(workbook);

                    ICell D10 = row_data.CreateCell(3);
                    D10.SetCellValue("-");
                    D10.CellStyle = Gx.GetStyle(workbook);

                    ICell E10 = row_data.CreateCell(4);
                    E10.SetCellValue(detail.DsLa?.ToString("F2") + "%");
                    E10.CellStyle = Gx.GetStyle(workbook);

                    ICell F10 = row_data.CreateCell(5);
                    F10.SetCellValue(detail.JsjmPosition?.ToString("F2"));
                    F10.CellStyle = Gx.GetStyle(workbook);

                    ICell G10 = row_data.CreateCell(6);
                    G10.SetCellValue(detail.ZjDia?.ToString("F2") + "%");
                    G10.CellStyle = Gx.GetStyle(workbook);

                    ICell H10 = row_data.CreateCell(7);
                    H10.SetCellValue(detail.JsjmPosition?.ToString("F2"));
                    H10.CellStyle = Gx.GetStyle(workbook);

                    ICell I10 = row_data.CreateCell(8);
                    I10.SetCellValue(detail.JsjmLma?.ToString("F2") + "%");
                    I10.CellStyle = Gx.GetStyle(workbook);

                    ICell J10 = row_data.CreateCell(9);
                    J10.SetCellValue(detail.NianJu?.ToString("F2"));
                    J10.CellStyle = Gx.GetStyle(workbook);

                    ICell K10 = row_data.CreateCell(10);
                    K10.SetCellValue(detail.NianJuChange?.ToString("F2") + "%");
                    K10.CellStyle = Gx.GetStyle(workbook);

                    ICell L10 = row_data.CreateCell(11);
                    L10.SetCellValue(detail.OthFuShi);
                    L10.CellStyle = Gx.GetStyle(workbook);

                    ICell M10 = row_data.CreateCell(12);
                    M10.SetCellValue(detail.OthMoSun);
                    M10.CellStyle = Gx.GetStyle(workbook);

                    ICell N10 = row_data.CreateCell(13);
                    N10.SetCellValue(detail.OthQiTa);
                    N10.CellStyle = Gx.GetStyle(workbook);

                    ICell O10 = row_data.CreateCell(14);
                    O10.SetCellValue(detail.OthZongHe);
                    O10.CellStyle = Gx.GetStyle(workbook);
                }

                //处理有多个位置信息
                foreach (var position in detail.CheckWireropeDetailPositions)
                {
                    IRow row_data = sheet.CreateRow(rowIndex++);
                    row_data.Height = Gx.GetExcelRowHeight(32);

                    ICell A10 = row_data.CreateCell(0);
                    A10.SetCellValue(detail.Name);
                    A10.CellStyle = Gx.GetStyle(workbook);

                    ICell B10 = row_data.CreateCell(1);
                    B10.SetCellValue(detail.DsCnt?.ToString());
                    B10.CellStyle = Gx.GetStyle(workbook);

                    ICell C10 = row_data.CreateCell(2);
                    C10.SetCellValue(position.DsStart.ToString("F2"));
                    C10.CellStyle = Gx.GetStyle(workbook);

                    ICell D10 = row_data.CreateCell(3);
                    D10.SetCellValue(position.DsEnd.ToString("F2"));
                    D10.CellStyle = Gx.GetStyle(workbook);

                    ICell E10 = row_data.CreateCell(4);
                    E10.SetCellValue(detail.DsLa?.ToString("F2") + "%");
                    E10.CellStyle = Gx.GetStyle(workbook);

                    ICell F10 = row_data.CreateCell(5);
                    F10.SetCellValue(detail.JsjmPosition?.ToString("F2"));
                    F10.CellStyle = Gx.GetStyle(workbook);

                    ICell G10 = row_data.CreateCell(6);
                    G10.SetCellValue(detail.ZjDia?.ToString("F2") + "%");
                    G10.CellStyle = Gx.GetStyle(workbook);

                    ICell H10 = row_data.CreateCell(7);
                    H10.SetCellValue(detail.JsjmPosition?.ToString("F2"));
                    H10.CellStyle = Gx.GetStyle(workbook);

                    ICell I10 = row_data.CreateCell(8);
                    I10.SetCellValue(detail.JsjmLma?.ToString("F2") + "%");
                    I10.CellStyle = Gx.GetStyle(workbook);

                    ICell J10 = row_data.CreateCell(9);
                    J10.SetCellValue(detail.NianJu?.ToString("F2"));
                    J10.CellStyle = Gx.GetStyle(workbook);

                    ICell K10 = row_data.CreateCell(10);
                    K10.SetCellValue(detail.NianJuChange?.ToString("F2") + "%");
                    K10.CellStyle = Gx.GetStyle(workbook);

                    ICell L10 = row_data.CreateCell(11);
                    L10.SetCellValue(detail.OthFuShi);
                    L10.CellStyle = Gx.GetStyle(workbook);

                    ICell M10 = row_data.CreateCell(12);
                    M10.SetCellValue(detail.OthMoSun);
                    M10.CellStyle = Gx.GetStyle(workbook);

                    ICell N10 = row_data.CreateCell(13);
                    N10.SetCellValue(detail.OthQiTa);
                    N10.CellStyle = Gx.GetStyle(workbook);

                    ICell O10 = row_data.CreateCell(14);
                    O10.SetCellValue(detail.OthZongHe);
                    O10.CellStyle = Gx.GetStyle(workbook);
                }
            }
            #endregion

            #region row_End
            IRow row_End = sheet.CreateRow(rowIndex);
            row_End.Height = Gx.GetExcelRowHeight(32);

            ICell A_End = row_End.CreateCell(0);
            A_End.SetCellValue($"检测日期：{lastCheck.CheckDate?.ToString("yyyy-MM-dd")}");
            A_End.CellStyle = Gx.GetStyle(workbook, horizontalAlignment: HorizontalAlignment.Left);
            Gx.MergeCell(rowIndex, rowIndex, 0, 3, sheet);

            ICell E_End = row_End.CreateCell(4);
            E_End.SetCellValue($"检测负责人：{lastCheck.CheckUserName}");
            E_End.CellStyle = Gx.GetStyle(workbook, horizontalAlignment: HorizontalAlignment.Left);
            Gx.MergeCell(rowIndex, rowIndex, 4, 10, sheet);

            ICell J_End = row_End.CreateCell(11);
            J_End.SetCellValue($"检测单位：{lastCheck.CheckUnit}");
            J_End.CellStyle = Gx.GetStyle(workbook, horizontalAlignment: HorizontalAlignment.Left);
            Gx.MergeCell(rowIndex, rowIndex, 11, 14, sheet);
            #endregion

            #region 插入图片
            rowIndex += 3;

            int rowCnt = 5;//一张图片占用行的数量
            int colCnt = 4;//一张图片占用列的数量

            foreach (var detail in lastCheck.CheckWireropeDetails)
            {
                var list = detail.Attr_Files.GetFileList();
                if (list.IsEmpty() || list.Count == 0)
                {
                    continue;
                }

                var urlList = list.Select(x => x.Url).ToList();
                urlList = urlList.Where(x => x.ToLower().EndsWith("jpg") || x.ToLower().EndsWith("jpeg") || x.ToLower().EndsWith("bmp") || x.ToLower().EndsWith("png") || x.ToLower().EndsWith("gif")).ToList();

                int j = 1;//从B这列开始
                foreach (var file in urlList)
                {
                    try
                    {
                        byte[] bytes = Gx.DownloadImageAsByteArray(file);
                        int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
                        XSSFDrawing patriarch = (XSSFDrawing)sheet.CreateDrawingPatriarch();
                        XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, j, rowIndex, j + colCnt, rowIndex + rowCnt - 1);
                        XSSFPicture pict = (XSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

                        j += colCnt;
                    }
                    catch
                    {
                        continue;
                    }
                }

                rowIndex += rowCnt;
            }
            #endregion

            using (FileStream fs = File.OpenWrite(uploadFile))
            {
                workbook.Write(fs);
            }

            workbook.Close();
            workbook.Dispose();

            return "/" + fileName;
        }
        #endregion

        #region 作废管理更新附件信息
        public void UpdateZuoFeiInfo(UsingWireropeModel model)
        {
            var entity = _repository.GetByID(model.UsingWireropeID);

            entity.OriginKn = model.OriginKn;
            entity.MinKn = model.MinKn;
            entity.MinKnPercent = model.MinKnPercent;
            entity.Attr1_Files = model.Attr1_FilesList.GetFileString();
            entity.Attr2_Files = model.Attr2_FilesList.GetFileString();
            entity.Attr3_Files = model.Attr3_FilesList.GetFileString();
            entity.Attr4_Files = model.Attr4_FilesList.GetFileString();

            _repository.Update(entity);
        }
        #endregion
    }
}
